﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace TSVNAddin.Config
{
	public class XmlFileStorage
	{
		public virtual T ReadObjectFromFile<T>(string fileName)
		{
			XmlSerializer serializer = new XmlSerializer(typeof(T));
			T obj = default(T);
			using (TextReader reader = File.OpenText(fileName))
			{
				obj = (T)serializer.Deserialize(reader);
			}
			return obj;
		}

		public virtual void WirteObjectToFile(object obj, string fileName)
		{
			string path = Path.GetDirectoryName(fileName);
			Directory.CreateDirectory(path);

			XmlSerializer serializer = new XmlSerializer(obj.GetType());
			using (FileStream stream = File.Create(fileName))
			{
				serializer.Serialize(stream, obj);
			}
		}

	}
}
